﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ADC.Utilites;
using ADC.BL;

namespace ADC.Web.Admin
{
    public partial class ListProjectType : System.Web.UI.Page
    {
        log4net.ILog logger = log4net.LogManager.GetLogger("ListProjectType");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Page.Title = Common.PAGE_TITLE + "Loại dự án";
                BindData();
            }
        }

        private void BindData()
        {
            try
            {
                var projectTypes = ProjectTypeFactory.GetAll();
                GridViewProjectType.AutoGenerateColumns = false;
                GridViewProjectType.DataSource = projectTypes;
                GridViewProjectType.DataBind();
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                if (GlobalInfo.IsEnableDebugMode) throw;
            }
        }

        protected void GridViewProjectType_RowEditing(object sender, GridViewEditEventArgs e)
        {
            try
            {
                var lblProjectTypeID = (Label)GridViewProjectType.Rows[e.NewEditIndex].FindControl("lblProjectTypeID");
                if (lblProjectTypeID != null && !string.IsNullOrEmpty(lblProjectTypeID.Text))
                {
                    Response.Redirect("ManageProjectType.aspx?Mode=1&ID=" + lblProjectTypeID.Text, false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                if (GlobalInfo.IsEnableDebugMode) throw;
            }
        }

        protected void GridViewProjectType_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                var lblProjectTypeID = (Label)GridViewProjectType.Rows[e.RowIndex].FindControl("lblProjectTypeID");
                if (lblProjectTypeID != null && !string.IsNullOrEmpty(lblProjectTypeID.Text))
                {
                    var projetTypeID = Convert.ToInt32(lblProjectTypeID.Text);

                    // Check if the current project Type does not have project
                    var numberOfproject = ProjectTypeFactory.CountProject(projetTypeID);

                    if (numberOfproject > 0)
                    {
                        // Show error message
                        Page.RegisterStartupScript("scriptAlert", "<script>alert('Bạn cần xóa các dự án thuộc loại dự án hiện tại!);</script> ");
                        return;
                    }
                    int result = ProjectTypeFactory.Delete(projetTypeID);
                    BindData();
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                if (GlobalInfo.IsEnableDebugMode) throw;
            }
        }
    }
}